<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-09-07 09:06:06
 * @Description: 租户级别，对应可以创建几个供应商、店铺(员工*2)账户
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-09-07 12:02:01
 * @FilePath: /qingshanERP/app/Http/Controllers/Console/Tenant/TenantLevelController.php
 */

namespace App\Http\Controllers\Console\Tenant;

use App\Http\Controllers\Controller;
use App\Models\Tenant\Tenant;
use App\Models\Tenant\TenantLevel;
use Illuminate\Http\Request;
use Validator;

class TenantLevelController extends Controller
{
    public function getList(Request $req)
    {
        try {
            $page = $req->input('page', 0);
            $size = $req->input('size', 10);
            $status = $req->input('status', '');
            $key = $req->input('key', '');
            $list = TenantLevel::with('parent')->where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('levle_name', 'like', '%' . $key . '%');
                }
            })->where(function ($q) use ($status) {
                if ($status != '') {
                    $q->where('status', $status);
                }
            })->where('del_flag', 0)->offset(($page - 1) * $size)->limit($size)->orderBy('id', 'desc')->get();
            $count = TenantLevel::where(function ($q) use ($key) {
                if ($key != '') {
                    $q->where('level_name', 'like', '%' . $key . '%');
                }
            })->where(function ($q) use ($status) {
                if ($status != '') {
                    $q->where('status', $status);
                }
            })->where('del_flag', 0)->count();
            $res = ['list' => $list, 'total' => $count];
            return $this->resData(200, '获取数据成功...', $res);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请稍后再试...');
        }
    }

    public function postCreate(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'parent_id' => 'required|integer',
                'level_name' => 'required|max:100',
                'shop_num' => 'required|numeric|min:1',
                'supplier_num' => 'required|numeric|min:1',
                'days' => 'required|numeric|min:1',
                'prices' => 'required|numeric|min:0'
            ]);
            $attrs = array(
                'parent_id' => '前一级别',
                'level_name' => '级别名称',
                'shop_num' => '店铺数量',
                'supplier_num' => '供应商数量',
                'days' => '有效期',
                'prices' => '价格'
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $all = $request->all();
            $create = ['parent_id' => $all['parent_id'],'level_name' => $all['level_name'], 'shop_num' => $all['shop_num'], 'supplier_num' => $all['supplier_num'], 'days' => $all['days'], 'prices' => $all['prices'], 'status' => 1];
            TenantLevel::create($create);
            return $this->resData(200, '添加成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '添加文章失败，请重新操作...');
        }
    }
    //详情
    public function postDetail(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'level_id' => 'required',
            ]);
            $attrs = array(
                'level_id' => '级别ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('level_id');
            $info = TenantLevel::find($id);
            return $this->resData(200, '获取数据成功...', $info);
        } catch (\Throwable $e) {
            return $this->resData(500, '获取数据失败，请重新操作...');
        }
    }
    //编辑
    public function postEdit(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'level_id' => 'required',
                'parent_id' => 'required|integer',
                'level_name' => 'required|max:100',
                'shop_num' => 'required|numeric|min:1',
                'supplier_num' => 'required|numeric|min:1',
                'days' => 'required|numeric|min:1',
                'prices' => 'required|numeric|min:0'
            ]);
            $attrs = array(
                'parent_id' => '前一级别',
                'level_id' => '级别ID',
                'level_name' => '级别名称',
                'shop_num' => '店铺数量',
                'supplier_num' => '供应商数量',
                'days' => '有效期',
                'prices' => '价格'
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('level_id');
            $all = $request->all();
            $parent = TenantLevel::where('parent_id', $all['parent_id'])->first();
            if (!is_null($parent) && $parent->id != $id) {
                return $this->resData(400, '不允许与其他级别同级');
            }
            // 循环判断下是不是自己的子集里边的一个
            if ($this->getAll($all['parent_id'], $id)) {
                return $this->resData(400, '不能选择自己的子级别');
            }
            if ($all['parent_id'] == $id) {
                return $this->resData(400, '不能选择自己作为自己的子级别');
            }
            $update = ['parent_id' => $all['parent_id'], 'level_name' => $all['level_name'], 'shop_num' => $all['shop_num'], 'supplier_num' => $all['supplier_num'], 'days' => $all['days'], 'prices' => $all['prices'], 'status' => $all['status'] == 'true' ? 1 : 0];
            TenantLevel::where('id', $id)->update($update);
            return $this->resData(200, '编辑成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '编辑失败，请重新操作...');
        }
    }
    // 找所有子集
    private function getAll($id, $myid){
        $parent = TenantLevel::where('id', $id)->value('parent_id');
        if ($parent == $myid) {
            return true;
        }else{
            if (is_null($parent)) {
                return false;
            } else {
                $this->getAll($parent, $myid);
            }
        }
    }
    //删除
    public function postRemove(Request $request)
    {
        try {
            $validator = Validator::make($request->input(), [
                'level_id' => 'required|array',
            ]);
            $attrs = array(
                'level_id' => '级别ID',
            );
            $validator->setAttributeNames($attrs);
            if ($validator->fails()) {
                // 如果有错误，提示第一条
                return $this->resData(400, $validator->errors()->all()[0] . '...');
            }
            $id = $request->input('level_id');
            // 验证下，如果级别下有租户，不让删除
            $has = Tenant::whereIn('tenant_level_id', $id)->where('del_flag',0)->where('status',1)->first();
            if (!is_null($has)) {
                return $this->resData(500, '级别下有租户，请先调整租户级别...');
            }
            $parent = Tenant::whereIn('parent_id', $id)->first();
            if (!is_null($parent)) {
                return $this->resData(400, '当前级别下有下级，不允许删除');
            }
            TenantLevel::whereIn('id', $id)->update(['del_flag' => 1]);
            return $this->resData(200, '删除成功...');
        } catch (\Throwable $e) {
            return $this->resData(500, '删除失败，请重新操作...');
        }
    }
}
